package GUI;

import utilities.RefreshListener;
import utilities.SpringUtilities;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import map.Resolution;


import concept.AttributesType;
import controller.ImageLoader;
import controller.LocalManager;

import Player.PG;

public class CurriculumPG extends JPanel{
    public CurriculumPG(RefreshListener refresh){
        this.setLayout(new BorderLayout());
        JLabel label = new JLabel("Curriculum PG");
        JPanel p = new JPanel();
        p.add(label);
        p.setOpaque(true);
        p.setBackground(new Color(160, 180, 200));
        this.refresh= refresh;
        this.player = LocalManager.getCurrentPlayer();
        this.attributesListener = new AttributesButtonListener();
        JPanel globalPanel = new JPanel();
        globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.Y_AXIS));
        globalPanel.add(createPanelInformation());
        globalPanel.add(new JSeparator(JSeparator.HORIZONTAL));
        globalPanel.add(createPanelAttributes());
        globalPanel.add(createPanelLifePoints());
        globalPanel.add(new JSeparator(JSeparator.HORIZONTAL));
        globalPanel.add(createPanelAvatar());
        this.add(p, BorderLayout.NORTH);
        this.add(globalPanel, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(350,600));
    }

    public JPanel createPanelInformation(){
        JLabel label = new JLabel("Informazioni");
        JPanel p = new JPanel();
        p.add(label);
        p.setOpaque(true);
        p.setBackground(new Color(200, 180, 160));
        level = new JTextField(player.getLevel()+"", 3);
        //money = new JTextField(player.getMoney()+"", 5);
        level.setOpaque(false); level.setEditable(false);
        //money.setOpaque(false); money.setEditable(false);
        JPanel panelInfo = new JPanel(new BorderLayout());
        

        JLabel name = new JLabel(player.getName());
        name.setAlignmentY(Component.CENTER_ALIGNMENT);
        name.setFont(new Font("Title", Font.BOLD, 40));
        JPanel p3 = new JPanel();
        p3.add(name);
        exp = new JProgressBar(0, player.getExpForLevel());
        exp.setValue(player.getExperience());
        exp.setStringPainted(true);
        exp.setToolTipText(player.getExperience()+"/"+player.getExpForLevel());
        JPanel p1 = new JPanel();
        p1.add(new JLabel("Livello: "));
        p1.add(level);
        p1.add(exp);
        JPanel p2 = new JPanel();
        p2.add(new JLabel("Razza: "));
        p2.add(new JLabel(player.getRace().getRaceName()));
        p2.add(new JLabel("   Fazione: "));
        p2.add(new JLabel(player.getBelongingFaction().getFactionName()));
        p2.add(new JSeparator(JSeparator.VERTICAL));
//        panelInfo1.add(new JLabel("Soldi: "));
//        JPanel p2 = new JPanel();
//        p2.add(money);
//        p2.add(new JLabel(" "+player.getBelongingFaction().getCoinName()));
//        panelInfo1.add(p2);
        JPanel centerPanel = new JPanel(new SpringLayout());
        centerPanel.add(p3);
        centerPanel.add(p1);
        centerPanel.add(p2);
        SpringUtilities.makeCompactGrid(centerPanel, 3, 1, 0, 0, 0, 0);
        panelInfo.add(p, BorderLayout.NORTH);
        panelInfo.add(centerPanel, BorderLayout.CENTER);
        return panelInfo;
    }

    public JPanel createPanelAttributes(){
        JLabel label = new JLabel("Attributi");
        JPanel p = new JPanel();
        p.add(label);
        p.setOpaque(true);
        p.setBackground(new Color(200, 180, 160));
        add0 = new JButton("+");
        add1 = new JButton("+");
        add2 = new JButton("+");
        add3 = new JButton("+");
        add0.addActionListener(attributesListener);
        add1.addActionListener(attributesListener);
        add2.addActionListener(attributesListener);
        add3.addActionListener(attributesListener);
        intelligence = new JTextField(player.getIntelligence()+"", 2);
        strength = new JTextField(player.getStrength()+"", 2);
        agility = new JTextField(player.getAgility()+"", 2);
        determination = new JTextField(player.getDetermination()+"", 2);
        unspentPoints = new JTextField(player.getUnspentAttributePoints()+"", 2);
        intelligence.setOpaque(false); intelligence.setEditable(false);
        strength.setOpaque(false); strength.setEditable(false);
        agility.setOpaque(false); agility.setEditable(false);
        determination.setOpaque(false); determination.setEditable(false);
        unspentPoints.setOpaque(false); unspentPoints.setEditable(false);
        JPanel panelAttr = new JPanel(new BorderLayout());
        JPanel panelAttr1 = new JPanel(new SpringLayout());
        panelAttr1.add(new JLabel("Intelligenza: "));
        panelAttr1.add(intelligence);
        panelAttr1.add(add0);
        panelAttr1.add(new JLabel("Forza: "));
        panelAttr1.add(strength);
        panelAttr1.add(add1);
        panelAttr1.add(new JLabel("Agilita: "));
        panelAttr1.add(agility);
        panelAttr1.add(add2);
        panelAttr1.add(new JLabel("Determinazione: "));
        panelAttr1.add(determination);
        panelAttr1.add(add3);
        SpringUtilities.makeCompactGrid(panelAttr1, 2, 6, 2, 5, 5, 2);
        JPanel supportPanel = new JPanel();
        supportPanel.add(panelAttr1);
        checkUnspentPoints();
        panelAttr.add(p, BorderLayout.NORTH);
        panelAttr.add(supportPanel, BorderLayout.CENTER);
        JPanel panel = new JPanel();
        panel.add(new JLabel("            punti da spendere: "));
        panel.add(unspentPoints);
        panelAttr.add(panel, BorderLayout.SOUTH);
        return panelAttr;
    }

    

    public JPanel createPanelLifePoints(){
        JLabel label = new JLabel("Punti Vita");
        JPanel p = new JPanel();
        p.add(label);
        p.setOpaque(true);
        p.setBackground(new Color(200, 180, 160));
        life = new JTextField(player.getLife()+"", 3);
        maxLife = new JTextField(player.getMaxLife()+"", 3);
        mana = new JTextField(player.getMana()+"", 3);
        maxMana = new JTextField(player.getMaxMana()+"", 3);
        life.setOpaque(false); life.setEditable(false);
        maxLife.setOpaque(false); maxLife.setEditable(false);
        mana.setOpaque(false); mana.setEditable(false);
        maxMana.setOpaque(false); maxMana.setEditable(false);
        JPanel panelLifePoints = new JPanel(new BorderLayout());
        JPanel panel = new JPanel(new SpringLayout());
        panel.add(new JLabel("Punti Vita: "));
        JPanel p1 = new JPanel();
        p1.add(life);
        p1.add(new JLabel("/"));
        p1.add(maxLife);
        panel.add(p1);
        panel.add(new JLabel("Punti Mana: "));
        JPanel p2 = new JPanel();
        p2.add(mana);
        p2.add(new JLabel("/"));
        p2.add(maxMana);
        panel.add(p2);
        SpringUtilities.makeCompactGrid(panel, 2, 2, 3, 5, 7, 2);
        JPanel supportPanel = new JPanel();
        supportPanel.add(panel);
        panelLifePoints.add(p, BorderLayout.NORTH);
        panelLifePoints.add(supportPanel, BorderLayout.CENTER);
        return panelLifePoints;
    }

    public JPanel createPanelAvatar(){
        JLabel label = new JLabel("Avatar + Storia");
        JPanel p = new JPanel();
        p.add(label);
        p.setOpaque(true);
        p.setBackground(new Color(200, 180, 160));
        JPanel panelAvatar = new JPanel(new BorderLayout());
        JPanel panel = new JPanel(new SpringLayout());
        String box = player.getAvatarBox();
        if(box != null)
        	panel.add(new JLabel(ImageLoader.getPlayerImage(Resolution.VVHIGH, box, player.getAvatarNum())));
        JTextArea t = new JTextArea(5,24);
        t.setLineWrap(true);
        t.setOpaque(false);
        t.append(player.getHistory());
        panel.add(t);
        SpringUtilities.makeCompactGrid(panel, box != null?2:1, 1, 3, 5, 0, 2);
        JPanel supportPanel = new JPanel();
        supportPanel.add(panel);
        panelAvatar.add(p, BorderLayout.NORTH);
        panelAvatar.add(supportPanel, BorderLayout.CENTER);
        return panelAvatar;
    }

    

    public void updateValues(){
        player.updateVariablesDependingOnAttributes();
        intelligence.setText(player.getIntelligence()+"");
        strength.setText(player.getStrength()+"");
        agility.setText(player.getAgility()+"");
        determination.setText(player.getDetermination()+"");
        unspentPoints.setText(player.getUnspentAttributePoints()+"");
        level.setText(player.getLevel()+"");
        //money.setText(player.getMoney()+"");
        life.setText(player.getLife()+"");
        maxLife.setText(player.getMaxLife()+"");
        mana.setText(player.getMana()+"");
        maxMana.setText(player.getMaxMana()+"");
        exp.setMaximum(player.getExpForLevel());
        exp.setValue(player.getExperience());
        exp.setToolTipText(player.getExperience()+"/"+player.getExpForLevel());
        exp.revalidate();
    }

    public void checkUnspentPoints(){
        if(player.getUnspentAttributePoints()<=0){
            add0.setVisible(false);
            add1.setVisible(false);
            add2.setVisible(false);
            add3.setVisible(false);
        }else{
        	add0.setVisible(true);
            add1.setVisible(true);
            add2.setVisible(true);
            add3.setVisible(true);
        }
    }

    public class AttributesButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JButton b = (JButton)e.getSource();
            if(b.equals(add0)){
                player.spentAnAttributePoint(AttributesType.INTELLIGENCE);
            }
            else if(b.equals(add1)){
            	player.spentAnAttributePoint(AttributesType.STRENGTH);
            }
            else if(b.equals(add2)){
            	player.spentAnAttributePoint(AttributesType.AGILITY);
            }
            else if(b.equals(add3)){
            	player.spentAnAttributePoint(AttributesType.DETERMINATION);
            }
            refresh.refresh();
        }
    }

    private PG player;
    private JButton add0, add1, add2, add3;
    
    private ActionListener attributesListener;
    private RefreshListener refresh;
    private JTextField intelligence, strength, agility, determination, unspentPoints, level, life, maxLife, mana, maxMana;
    private JProgressBar exp;
    private static final long serialVersionUID = 1L;
}
